var personProfile = personProfile || {};

function InitPersonProfileList() {
	fillPersonProfileData();
}

var habits = ["朝九晚五", "夜猫子", "自由自在，无规律", "处为父母", "小宝宝", "学生", "退休在家", "其他"];
var allergy = ["花粉", "酒", "海鲜", "其他"];
var disease = ["糖尿病", "高血压", "高血脂", "冠心病", "其他"];
var eatingHabit = ["不吃早点", "不定时吃饭", "喜欢辛辣", "喜欢吃肉", "喜欢吃素", "喜欢零食", "喝水较少", "减肥中"];

function selectLivingHabits() {
	var body = "";
	var livingHabits = "";
	for (var i = 0; i < habits.length; i++) {
		var item = " <input name='habits' id='habit-" + i + "' type='checkbox' value='" + habits[i] + "'>";
		item = item + " <label for='habit-" + i + "'>" + habits[i] + "</label>";
		body = body + item;
	}

	var popupOpt = {
		title: "生活习惯",
		message: body,
		cancelText: "取消",
		cancelCallback: null,
		doneText: "确定",
		doneCallback: function() {
			var selectedString;
			var $inputs = $("input[name='habits']");
			var reg = /,$/gi;
			$("input[name='habits']").each(function() {
				if ($(this)[0].checked) {
					livingHabits = livingHabits + $(this).val() + ",";
				}
			});

			livingHabits = livingHabits.replace(reg, "");

			personProfile.life = livingHabits; //updates personProfile object

			$(".user-living-habit").val(livingHabits);
		},
		cancelOnly: false,

	};
	$.ui.popup(popupOpt);
}

function selectAllergy() {
    var body = "";
	var allergy = "";
	for (var i = 0; i < allergy.length; i++) {
		var item = " <input name='allergy' id='allergy-" + i + "' type='checkbox' value='" + allergy[i] + "'>";
		item = item + " <label for='allergy-" + i + "'>" + allergy[i] + "</label>";
		body = body + item;
	}

	var popupOpt = {
		title: "过敏",
		message: body,
		cancelText: "取消",
		cancelCallback: null,
		doneText: "确定",
		doneCallback: function() {
			var selectedString;
			var $inputs = $("input[name='allergy']");
			var reg = /,$/gi;
			$("input[name='allergy']").each(function() {
				if ($(this)[0].checked) {
					allergy = allergy + $(this).val() + ",";
				}
			});

			allergy = allergy.replace(reg, "");

			personProfile.allergy = allergy; //updates personProfile object

			$(".user-user-allergy").val(allergy);
		},
		cancelOnly: false,

	};
	$.ui.popup(popupOpt);
}

function selectIssues() {
    var body = "";
	var issues = "";
	for (var i = 0; i < allergy.length; i++) {
		var item = " <input name='issues' id='issues-" + i + "' type='checkbox' value='" + disease[i] + "'>";
		item = item + " <label for='issues-" + i + "'>" + disease[i] + "</label>";
		body = body + item;
	}

	var popupOpt = {
		title: "慢性病",
		message: body,
		cancelText: "取消",
		cancelCallback: null,
		doneText: "确定",
		doneCallback: function() {
			var reg = /,$/gi;
			$("input[name='issues']").each(function() {
				if ($(this)[0].checked) {
					issues = issues + $(this).val() + ",";
				}
			});
			issues = issues.replace(reg, "");

			personProfile.issues = issues; //updates personProfile object

			$(".user-user-disease").val(issues);
		},
		cancelOnly: false,

	};
	$.ui.popup(popupOpt);
}

function selectEatingHabbits() {
    var body = "";
	var eatings = "";
	for (var i = 0; i < eatingHabit.length; i++) {
		var item = " <input name='eating-habit' id='eating-habit-" + i + "' type='checkbox' value='" + eatingHabit[i] + "'>";
		item = item + " <label for='eating-habit-" + i + "'>" + eatingHabit[i] + "</label>";
		body = body + item;
	}

	var popupOpt = {
		title: "饮食习惯",
		message: body,
		cancelText: "取消",
		cancelCallback: null,
		doneText: "确定",
		doneCallback: function() {
			var selectedString;
			var $inputs = $("input[name='eating-habit']");
			var reg = /,$/gi;
			$("input[name='eating-habit']").each(function() {
				if ($(this)[0].checked) {
					eatings = eatings + $(this).val() + ",";
				}
			});

			eatings = eatings.replace(reg, "");

			personProfile.eat = eatings; //updates personProfile object

			$(".user-eating-habit").val(eatings);
		},
		cancelOnly: false,

	};
	$.ui.popup(popupOpt);
}

function fillPersonProfileData() {

	personProfile = $N.ctrl.PersonProfileCtrl.getSelectedData();
  
	console.log("personProfile = ");
	console.log(personProfile);
	$(".user-name").attr("value", personProfile.name);
	$(".user-age").attr("value", personProfile.age);
	$(".user-height").attr("value", personProfile.height);
	$(".user-weight").attr("value", personProfile.weight);

	if (personProfile.sex == "男") {
		$(".user-gender").attr("checked", "checked");
	}

	$(".user-living-habit").attr("value", personProfile.life);
	$(".user-eating-habit").attr("value", personProfile.eat);
	$(".user-disease").attr("value", personProfile.disease);
	$(".user-issues").attr("value", personProfile.issues);
	$(".user-allergy").attr("value", personProfile.allergy);

	var birth = new Date();
	birth.setFullYear(personProfile.birthYear);
	birth.setMonth(personProfile.birthMonth);
	birth.setDate(personProfile.birthDay);

	$(".user-birth-date").attr("value", birth.format("yyyy-MM-dd"));
}

function uploadPersonProfile() {
	$N.ctrl.PersonProfileCtrl.uploadPersonProfileData(personProfile);
}